package com.xfsong.demo.service.impl;
import com.xfsong.demo.entity.Category;
import com.xfsong.demo.mapper.CategoryMapper;
import com.xfsong.demo.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int insertCategory(Category category) {
        return categoryMapper.insertCategory(category);
    }

    @Override
    public int deleteCategoryById(Integer id) {
        return categoryMapper.deleteCategoryById(id);
    }

    @Override
    public int updateCategory(Category category) {
        return categoryMapper.updateCategory(category);
    }

    @Override
    public Category selectCategoryById(Integer id) {
        return categoryMapper.selectCategoryById(id);
    }

    @Override
    public List<Category> selectAllCategories() {
        return categoryMapper.selectAllCategories();
    }

    @Override
    public boolean isCategoryNameExists(String categoryName) {
        return categoryMapper.selectCategoryByName(categoryName) != null;
    }
    @Override
    public boolean canDeleteCategory(Integer id) {
        if (id == null) {
            return false;
        }
        int productCount = categoryMapper.countProductsByCategoryId(id);
        return productCount == 0;
    }

    @Override
    public Category selectCategoryByName(String categoryName) {
        return categoryMapper.selectCategoryByName(categoryName);
    }

    @Override
    public List<Category> selectCategoriesByPageAndName(int page, int pageSize, String categoryName) {
        int offset = (page - 1) * pageSize;
        return categoryMapper.selectCategoriesByPageAndName(offset, pageSize, categoryName);
    }

    @Override
    public int countCategoriesByName(String categoryName) {
        return categoryMapper.countCategoriesByName(categoryName);
    }
}
